package com.heima.article.controller;

import com.heima.article.entity.ApArticle;
import com.heima.article.es.ApArticleSearch;
import com.heima.article.es.ApArticleSearchRepository;
import com.heima.article.service.IApArticleService;
import com.heima.common.dto.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.GetQuery;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private IApArticleService articleService;

    /*
        创建es索引
     */
    @GetMapping("/createIndex")
    public ResponseResult createIndex(){
        elasticsearchRestTemplate.createIndex(ApArticleSearch.class);
        System.out.println("创建索引成功");
        return ResponseResult.okResult();
    }

    /*
        创建mapping映射关系
     */
    @GetMapping("/putMapping")
    public ResponseResult putMapping(){
        elasticsearchRestTemplate.putMapping(ApArticleSearch.class);
        System.out.println("创建mapping映射成功");
        return ResponseResult.okResult();
    }

    /*

     */
    @GetMapping("es/add/{id}")
    public ResponseResult addDoc(@PathVariable(value = "id") Long id){
        ApArticle article = articleService.getById(id);
        ApArticleSearch articleSearch = new ApArticleSearch();
        BeanUtils.copyProperties(article,articleSearch);
        //保存到es中
        IndexQuery query = new IndexQuery();
        query.setId(articleSearch.getId().toString());
        query.setObject(articleSearch);
        elasticsearchRestTemplate.index(query);
        return ResponseResult.okResult("添加文档成功");
    }

    /*
        保存文章列表
     */
    @GetMapping("/addDoc")
    public ResponseResult addDocx(){
        //将数据同步到es中
        List<ApArticle> apArticles = articleService.list();
        for (ApArticle article : apArticles) {
            ApArticleSearch articleSearch = new ApArticleSearch();
            BeanUtils.copyProperties(article,articleSearch);
            //保存到es中
            IndexQuery query = new IndexQuery();
            query.setId(articleSearch.getId().toString());
            query.setObject(articleSearch);
            elasticsearchRestTemplate.index(query);
        }
        System.out.println("添加文章列表成功");
        return ResponseResult.okResult("添加文章列表成功");
    }

    @Autowired
    ApArticleSearchRepository articleSearchRepository;
    /*
        删除
     */
    @GetMapping("/del")
    public ResponseResult delAll(){
        articleSearchRepository.deleteAll();
        return ResponseResult.okResult("删除成功");
    }

    /*
        用SpringDataElasticsearch 的仓库接口添加文档
     */

    @GetMapping("/addAll")
    public ResponseResult addAll(){
        List<ApArticle> articles = articleService.list();
        List<ApArticleSearch> list = new ArrayList<>();
        for (ApArticle article : articles) {
            ApArticleSearch articleSearch = new ApArticleSearch();
            BeanUtils.copyProperties(article,articleSearch);
            list.add(articleSearch);
        }
        articleSearchRepository.saveAll(list);
        return ResponseResult.okResult("保存成功");
    }

    @GetMapping("/{id}")
    public ResponseResult findById(@PathVariable(value = "id") String id){
        GetQuery query = new GetQuery();
        query.setId(id);
        ApArticleSearch search = elasticsearchRestTemplate.queryForObject(query, ApArticleSearch.class);
        System.out.println(search);
        return ResponseResult.okResult("查询完成");
    }
}
